package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Tpermission;
import model.Trolepermission;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pageModel.Permission;
import dao.PermissionDaoI;
import dao.RolePermissionDaoI;
import service.PermissionServiceI;
@Service("permissionService")
public class PermissionServiceImpl implements PermissionServiceI{
private PermissionDaoI permissionDao;
private RolePermissionDaoI rolePermissonDao;
	public PermissionDaoI getPermissionDao() {
	return permissionDao;
}
@Autowired
public void setPermissionDao(PermissionDaoI permissionDao) {
	this.permissionDao = permissionDao;
}

	public RolePermissionDaoI getRolePermissonDao() {
	return rolePermissonDao;
}
@Autowired
public void setRolePermissonDao(RolePermissionDaoI rolePermissonDao) {
	this.rolePermissonDao = rolePermissonDao;
}
	@Override
	public List<Permission> showPermission(String roleid) {
		List<Permission> lperm = new ArrayList<>();
		String hql="from Trolepermission t where t.trole.id=:id";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("id", roleid);
		List<Trolepermission> l = rolePermissonDao.find(hql, params);
		for(Trolepermission t : l){
			String hql1="from Tpermission t where t.type='0' and t.id=:pid";
			Map<String,Object> params1=new HashMap<String,Object>();
			params1.put("pid", t.getTpermission().getId());
			List<Tpermission> p = permissionDao.find(hql1,params1);
			for(Tpermission tp : p){
				Permission perm = new Permission();
				BeanUtils.copyProperties(tp, perm);
				lperm.add(perm);
			}
		}
		return lperm;
	}

}
